package com.hex.liyue.giftmodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.giftmodule.bean.MyGiftsResultBean;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterMyGifts extends RecyclerView.Adapter<AdapterMyGifts.MyHolder> {
    private MyGiftsResultBean myGiftsResultBean;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onCheckBoxClickListener();
    }

    public AdapterMyGifts(MyGiftsResultBean myGiftsResultBean) {
        this.myGiftsResultBean = myGiftsResultBean;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_my_gifts, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        final MyGiftsResultBean.DataBean dataBean = myGiftsResultBean.data.get(position);
        Glide.with(LiYueApplication.getInstance())
                .load(dataBean.gift_url)
                .error(R.drawable.no_icon)
                .into(holder.iv_gift_icon);
        holder.tv_gift_brand.setText(dataBean.gift_brand);
        holder.tv_gift_name.setText(dataBean.gift_name);
        holder.tv_price.setText("￥" + dataBean.price);
        dataBean.current_gift_num = dataBean.gift_num;
        holder.tv_gift_num.setText("" + dataBean.current_gift_num);
        String user_type = UserManager.getUserType();
        if (user_type.equals("1")) {//普通用户  you 50    40
            if (!TextUtils.isEmpty(UserManager.getInviteCode())) {
                //有上线
                dataBean.zhexianPrice = dataBean.price * 0.5;
            } else {
                dataBean.zhexianPrice = dataBean.price * 0.4;
            }
        } else if (user_type.equals("2")) {//公司  60
            dataBean.zhexianPrice = dataBean.price * 0.6;

        } else if (user_type.equals("3")) {//大区  64
            dataBean.zhexianPrice = dataBean.price * 0.64;
        }

        holder.tv_priceed.setText("折现金额￥" + dataBean.zhexianPrice);
        if (dataBean.isCheck) {
            holder.iv_my_gifts.setImageResource(R.drawable.checked);
        } else {
            holder.iv_my_gifts.setImageResource(R.drawable.unchecked);
        }
        holder.iv_my_gifts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBean.isCheck) {
                    holder.iv_my_gifts.setImageResource(R.drawable.unchecked);
                    dataBean.isCheck = false;
                } else {
                    holder.iv_my_gifts.setImageResource(R.drawable.checked);
                    dataBean.isCheck = true;
                }
                onItemClickListener.onCheckBoxClickListener();
            }
        });
        holder.tv_gift_num_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBean.current_gift_num > 1 && dataBean.isCheck) {
                    dataBean.current_gift_num--;
                    holder.tv_gift_num.setText("" + dataBean.current_gift_num);
                }
            }
        });
        holder.tv_gift_num_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBean.current_gift_num < dataBean.gift_num && dataBean.isCheck) {
                    dataBean.current_gift_num++;
                    holder.tv_gift_num.setText("" + dataBean.current_gift_num);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return myGiftsResultBean.data.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView iv_my_gifts, iv_gift_icon;
        TextView tv_gift_brand, tv_gift_name, tv_price, tv_priceed, tv_gift_num, tv_gift_num_delete, tv_gift_num_add;

        public MyHolder(View itemView) {
            super(itemView);
            iv_my_gifts = (ImageView) itemView.findViewById(R.id.iv_my_gifts);
            iv_gift_icon = (ImageView) itemView.findViewById(R.id.iv_gift_icon);
            tv_gift_brand = (TextView) itemView.findViewById(R.id.tv_gift_brand);
            tv_gift_name = (TextView) itemView.findViewById(R.id.tv_gift_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_priceed = (TextView) itemView.findViewById(R.id.tv_priceed);
            tv_gift_num = (TextView) itemView.findViewById(R.id.tv_gift_num);
            tv_gift_num_delete = (TextView) itemView.findViewById(R.id.tv_gift_num_delete);
            tv_gift_num_add = (TextView) itemView.findViewById(R.id.tv_gift_num_add);
        }
    }
}
